<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/22 0022
 * Time: 下午 2:15
 */
namespace app\model;
class UserJobSend extends \think\Model{
    public $table="user_job_send";

    /**
     * 是否已经投递过这工作
     */
    function if_deliver($user_id,$job_id){
        $o=$this->where(['user_id'=>$user_id,'job_id'=>$job_id])->column("id");
        if($o[0]==true){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 添加一条简历投递记录
     * @param $user_id 用户ID
     * @param $job_id 工作ID
     * @param $user_resume_id 简历ID
     * @return int|string
     */
    function add_deliver($user_id,$job_id,$user_resume_id){
        $data['user_id']=$user_id;
        $data['job_id']=$job_id;
        $data['user_resume_id']=$user_resume_id;
        $data['created_at']=cur_time();
        $data['status']=0;



        $UserResume=new UserResume();
        $UserResumeEdu=new UserResumeEdu();
        $UserResumeExp=new UserResumeExp();
        $UserResumeCertificate=new UserResumeCertificate();
        $info['base']=$UserResume->get_info($user_id);
        $info['edu']=$UserResumeEdu->get_arrs_by_user_id($user_id);
        $info['exp']=$UserResumeExp->get_arrs_by_user_id($user_id);
        $info['cer']=$UserResumeCertificate->get_arrs_by_user_id($user_id);

        $data['user_resume']=json_encode($info);

        $Job=new Job();
        $job_info=$Job->get_job_info_by_id($job_id);

        $data['job_name']=$job_info['job_name'];
        $data['company_user_id']=$job_info['user_id'];
        $data['company_name']=$job_info['company']['company_name'];
        $data['job']=json_encode($job_info);
        return $this->insert($data,false,true);

    }

    /**
     * 取得企业用户已经收到的简历
     * @param int $page
     * @param $compnay_user_id
     * @return mixed
     */
    function get_has_deliver_resume($page=1,$compnay_user_id){
        $offset=($page-1)*10;
        $o=$this->where(['company_user_id'=>$compnay_user_id])->limit($offset,10)->order("id","desc")->select();
        $arrs=object2array($o);
        foreach($arrs as $k=>$v){
            $arrs[$k]['user_resume']=json_decode($v['user_resume']);
        }
        return $arrs;

    }

    /**
     * 返回指定用户的投递工作简历记录
     * @param int $page
     * @param $user_id
     * @return mixed
     */
    function get_has_deliver_job_list($page=1,$user_id){
        $offset=($page-1)*10;
        $o=$this->where(['user_id'=>$user_id])->limit($offset,10)->select();
        $arrs=object2array($o);
        foreach($arrs as $k=>$v){
            //$arrs[$k]['job']=json_decode($v['job']);
            $arrs[$k]['status_name']=config('web.user_job_send_status')[$v['status']];
            unset($arrs[$k]['job']);
            unset($arrs[$k]['user_resume']);
        }
        return $arrs;
    }

    /**
     * 根据主键ID,返回人简历
     * @param $id
     * @return array|mixed
     */
    function get_has_deliver_resume_by_id($id){
        $o=$this->where(['id'=>$id])->field("id,user_resume")->find();
        $arr=object2array($o);
        if($arr){
            $arr['user_resume']=json_decode($arr['user_resume']);

            return $arr;
        }else{
            return [];
        }
    }

    /**
     * 记录变成已查看
     * @param $id
     * @return $this|bool
     */
    function if_save_check($id){
        $o=$this->where(['id'=>$id])->column('status');
        if($o[0]==0){
            return $this->where(['id'=>$id])->update(['status'=>1,'updated'=>cur_time()]);
        }else{
            return true;
        }
    }

    /**
     * 根据主键ID，返回对应的用户ID
     * @param $id
     * @return mixed
     */
    function get_user_id_by_id($id){
        $o=$this->where(['id'=>$id])->column('user_id');
        return $o[0];
    }

    /**
     * 通过主键，返回他的全部信息记录
     * @param $id
     * @return mixed
     */
    function get_info_by_id($id){
        $o=$this->where(['id'=>$id])->find();
        return object2array($o);
    }
}